import { Window_Selectable } from "@/windows/Window_Selectable.js";

export class Window_Command extends Window_Selectable{
    constructor(width,maxHeight,autoInit=true){
        super(width,maxHeight,false);
        if(autoInit){
            this.init(width,maxHeight);
        }
    }

    init(width,height){
        this.okHandler=null;
        this.cancelHandler=null;
        /**
         * @type {{
         * name,
         * callback,
         * disabled,
         * index
         * }[]}
         */
        this.list=[];
        this.listMap={};

        this.makeCommands();
        height=Math.min(this.roundToLineHeight(height),this.itemHeight()*this.length()+2*this._padding);
        super.init(width,height);
    }

    destroy(opts){
        super.destroy(opts);
        this.okHandler=null;
        this.cancelHandler=null;
        this.list.length=0;
        this.listMap={};
    }
    
    row() { return this.length(); }
    viewRow() { return Math.ceil(this.contentsHeight()/this.itemHeight()); }
    length(){return this.list.length;}

    itemEnabled(index){
        let item=this.list[index];
        if(item){
            return !item.disabled;
        }
        return false;
    }

    addCommand(name,callback,disabled=false){
        let item=this.listMap[name];
        if(!item){
            this.listMap[name]={name,callback,disabled,index:this.list.length};
            this.list.push(this.listMap[name]);
        }else{
            item.callback=callback;
            item.disabled=disabled;
            if(item.disabled!=disabled){
                item.disabled=disabled;
                this.refreshItem(index);
            }
        }
    }

    setHandler(name,callback){
        if(!callback)return;
        let item=this.listMap[name];
        if(item){
            item.callback=callback;
        }else if(name=='ok'){
            this.okHandler=callback;
        }else if(name=='cancel'){
            this.cancelHandler=callback;
        }
    }

    removeCommand(name){
        let item=this.listMap[name];
        if(item){
            for(let i=item.index+1;i<this.list.length;++i){
                --this.list[i].index;
            }
            this.list.splice(item.index,1);
            delete this.listMap[name];
            if(this.viewContain(item.index)){
                this.refresh();
            }
        }
    }

    makeCommands(){}
    onResponse(index){
        if(index<this.list.length){
            let item=this.list[index];
            if(item.callback){
                item.callback(item,index);
            }else{
                this.okHandler?.(item,index);
            }
        }
    }

    onCancel(){
        this.cancelHandler?.();
    }

    drawItem(index,x,y,width,height){
        let item=this.list[index];
        if(item){
            if(item.disabled){
                this.changePaintOpacity(false);
            }
            this.drawText(item.name,x,y,width,height);
            if(item.disabled){
                this.changePaintOpacity(true);
            }
        }
    }

    roundToLineHeight(height){
        let ctxHeight=height-2*this._padding;
        return (ctxHeight-ctxHeight%this.itemHeight())+this._padding*2;
    }

    currentCommand(){
        return this.list[this.selectedIndex]?.name;
    }
}